
import { DeviceServiceDetailResult, Icategory } from './interface'


export function statedata(value: any, category: Icategory, devStatusCfg: DeviceFunctionServiceCategoriesResult['functions'] = {}, unit: boolean = true) {
    if (category.type === 'switchs') {
        return value ? '开' : '关'
    } else if (category.type === 'state') {
        if (category.code === 'alarm_message') {
            return '已上报'
        }
        return value
    } else if (category.type === 'select') {
        return category.options?.find(c => c.value === value)?.label || ''
    }

    else if (category.type === 'slider') {
        // 根据规格属性集合的单位进行转换
        // { "min": 11, "unit": "s", "scale": 0, "max": 86400, "step": 1 }
        // console.log("statedata:", value, category, devStatusCfg)
        const cfg = JSON.parse(devStatusCfg?.values || '{}')
        value = slidervalue(Number(value), cfg?.scale).toFixed(2)
        // if (['humidity', 'temp_current', 'va_temperature', 'va_humidity', 'maxtemp_set', 'minitemp_set', 'maxhum_set', 'minihum_set'].includes(category.code)) {
        //     value = parseFloat((Number(value) / 10).toFixed(2))
        // }
        if (unit && value) {
            return cfg?.unit ? value + cfg?.unit : value
        }
        return value
    }
    return value
}

export const bizCode: Record<string, string> = {
    online: '设备上线',
    offline: '设备离线',
    nameUpdate: '修改设备名称',
    dpNameUpdate: '修改设备功能点名称',
    bindUser: '设备绑定用户',
    delete: '删除设备',
    upgradeStatus: '设备升级状态',
}


export function statedata2(value: any, category: DeviceFunctionServiceCategoriesResult['functions'] = {}, dev: DeviceServiceDetailResult) {
    const values = JSON.parse(category?.values || '{}')
    if (['slider', 'Integer'].includes(category.type)) {
        value = slidervalue(Number(value), values.scale).toFixed(1)
        if (values?.unit) {
            return values.unit ? value + values.unit : value
        }
        return value
    }
    else if (['select', 'Enum'].includes(category.type)) {
        return category.options?.find(c => c.value === value)?.label || ''
    }
    if (['switchs', 'Boolean'].includes(category.type)) {
        return value ? '开' : '关'
    }
    if (category.type === 'state') {
        if (category.code === 'alarm_message') {
            return '已上报'
        }
        return value
    }
    if (values?.unit) {
        return value + values.unit
    }
    return value
}

export const slidervalue = (value: number, scale: number = 0) => {
    return value / Math.pow(10, scale)
}
// 滑块
export const _slidervalue = (value: number, scale: number = 0) => {
    return value * Math.pow(10, scale)
}
